﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GP.Logic;
using GP.Logic.Model;

namespace WebForms.Doctor
{
    public partial class GrantAccess : System.Web.UI.Page
    {
        private readonly DoctorService _doctorService = new DoctorService();

        protected void Page_Load(object sender, EventArgs e)
        {
            Citizen currentCitizen = _doctorService.GetCitizen();

            GP.Logic.Model.Doctor doctor = _doctorService.GetDoctor();

            if (currentCitizen == null || doctor == null || currentCitizen.DocId != doctor.DocId)
            {
                Response.Redirect("/Doctor/", true);
                return;
            }
            
            Name.Text = currentCitizen.Name.ToString();
            NameValue.Text = currentCitizen.Name.ToString();
            SSV.Text = currentCitizen.SocialSecurity.ToString();
            //chooseDoctor.DataSource = dr.GetDoctorsExcept();
            
            if (!IsPostBack)
            {
                UpdateBinding();
            }
        }

        private void UpdateBinding()
        {
            
            long citizenId = long.Parse(Request["citizenId"].ToString());
            chooseDoctor.DataSource = _doctorService.GetDoctorsWhichDontHaveGrantedAccess(citizenId);
            chooseDoctor.DataBind();
        }

        protected void cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Doctor/Patient.aspx?CitizenID=" + Request["citizenID"], false);
        }

        protected void verifyGrant_Click(object sender, EventArgs e)
        {
            CitizenService cS = new CitizenService();
            long citizenId = long.Parse(Request["CitizenId"].ToString());
            long docId = long.Parse(chooseDoctor.SelectedValue);
            DateTime expires = DateTime.Parse(chosenDate.Text);
            cS.CreateAccess(citizenId, docId, expires);
            UpdateBinding();
        }

        protected void chooseDate_SelectionChanged(object sender, EventArgs e)
        {
            chosenDate.Text = chooseDate.SelectedDate.ToString();
        }
    }
}